﻿namespace CommandTools
{
    /// <summary>
    /// 命令对象：包含命令的名称，帮助信息以及命令所调用的方法委托
    /// </summary>
    public class Command
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">命令的名称</param>
        /// <param name="toDo">命令执行的操作</param>
        public Command(string name, ToDo toDo)
        {
            Name = name;
            Operation = toDo;
            CommandHelpDoc = new CommandHelpDoc();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">命令的名称</param>
        /// <param name="toDo">命令执行的操作</param>
        public Command(string name, ToDo toDo, CommandHelpDoc commandHelpDoc)
        {
            Name = name;
            Operation = toDo;
            CommandHelpDoc = commandHelpDoc ?? new CommandHelpDoc();
        }

        /// <summary>
        /// 定义执行操作的委托
        /// </summary>
        /// <param name="args">执行操作的参数列表</param>
        public delegate void ToDo(string[] args);

        /// <summary>
        /// 命令执行的操作
        /// </summary>
        public ToDo Operation { get; private set; }

        /// <summary>
        /// 命令的名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 命令的帮助对象
        /// </summary>
        public CommandHelpDoc CommandHelpDoc { get; set; }

        /// <summary>
        /// 获取命令的帮助文档
        /// </summary>
        public string HelpDoc => CommandHelpDoc.ToConsoleDoc();

        /// <summary>
        /// 执行命令所对应的有参操作
        /// </summary>
        /// <param name="args">执行操作的参数列表</param>
        public void Execute(string[] args)
        {
            Operation(args);
        }

        /// <summary>
        /// 执行命令所对应的无参操作
        /// </summary>
        public void Execute()
        {
            Operation(null);
        }

    }
}
